<template>
	<view class="addressItem">
		<view class="addressInfo">
			<view class="address">{{ addressInfo.province.name }} {{ addressInfo.city.name }} {{ addressInfo.district.name }}</view>
			<view class="address-info">{{ addressInfo.address }}</view>
			<view class="userInfo">
				<text>{{ addressInfo.name }}</text>
				<text class="iphone">{{ addressInfo.phone }}</text>
				<uni-tag text="默认" v-if="addressInfo.default == '1'" size="small" type="error"></uni-tag>
			</view>
		</view>
		<view class="addressInfo-btn" @click="eidtAddress(addressInfo.id)">
			<u-icon name="edit-pen-fill" color="#999" size="20"></u-icon>
			<text>编辑</text>
		</view>
		<view class="addressInfo-btn" @click="getuserAddressDelete(addressInfo.id)">
			<u-icon name="trash" color="#999" size="20"></u-icon>
			<text>删除</text>
		</view>
	</view>
</template>

<script>
	import {userAddressDelete} from "../../api/home/index.js"
export default {
	name: 'hwxcAddress',
	data() {
		return {};
	},
	props: {
		addressInfo: {
			type: Object,
			default: () => {}
		}
	},
	methods: {
		// 跳转地址编辑
		eidtAddress(id) {
			uni.navigateTo({
				url: `/pages/addAddress/addAddress?type=eidt&id=${id}`
			});
		},
		
		async getuserAddressDelete(id){
			try{
				const resData = await userAddressDelete({
					id
				})
				if(resData.code == 1){
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					});
					setTimeout(()=>{
						this.$emit('refresh')
					},500)
				}else{
					uni.showToast({
						title: resData.msg,
						icon: 'none'
					});
				}
			}catch(e){
				throw Error(e)
				//TODO handle the exception
			}
		}
	}
};
</script>

<style scoped lang="scss">
	.addressItem {
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
		padding: 30rpx 0px;
		border-bottom: 1px #f3f3f3 solid;
		.addressInfo {
			flex: 1;
			.address {
				font-size: 24rpx;
				color: #999;
			}
	
			.address-info {
				font-size: 26rpx;
				font-weight: bold;
				margin: 12rpx 0;
				color: #444;
			}
	
			.userInfo {
				font-size: 26rpx;
				display: flex;
				align-items: center;
				.iphone {
					padding: 0px 20rpx;
				}
			}
		}
	
		.addressInfo-btn {
			font-size: 28rpx;
			width: 70rpx;
			margin-left: 10rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			font-size: 24rpx;
			color: #444;
		}
	}
</style>
